package com.easyShop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyShop.common.QueryPageParam;
import com.easyShop.common.Result;
import com.easyShop.entity.OrderReturnReason;
import com.easyShop.service.OrderReturnReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 退货原因表 前端控制器
 * </p>
 *
 * @author wms
 * @since 2024-12-04
 */
@RestController
@RequestMapping("/returnApply")
public class OrderReturnReasonController {
    @Autowired
    private OrderReturnReasonService orderReturnReasonService;

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody OrderReturnReason orderReturnReason){
        orderReturnReason.setCreateTime(new Date());
        return orderReturnReasonService.save(orderReturnReason)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody OrderReturnReason orderReturnReason){
        return orderReturnReasonService.updateById(orderReturnReason)?Result.suc():Result.fail();
    }

    //更新单个记录启用状态
    @PostMapping("/update/status")
    public Result updateStatus(@RequestParam Integer id,@RequestParam Integer status){
        OrderReturnReason updateStatus = new OrderReturnReason();
        updateStatus.setId(id);
        updateStatus.setStatus(status);
        return orderReturnReasonService.updateById(updateStatus)?Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return orderReturnReasonService.removeById(id)?Result.suc():Result.fail();
    }
    //获取分页列表
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");

        Page<OrderReturnReason> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<OrderReturnReason> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(OrderReturnReason::getName,name);
        }

        IPage result = orderReturnReasonService.pageCC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }
    //显示or查看
    @GetMapping("/list/{status}")
    public Result list(@PathVariable String status) {
        Integer statusInt = Integer.parseInt(status);
        LambdaQueryWrapper<OrderReturnReason> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderReturnReason::getStatus, statusInt);
        List list = orderReturnReasonService.list(queryWrapper);
        return Result.suc(list);
    }
}
